/**
 *
 */
package ua.at.tsvetkov.samizdat.adapters;

import java.util.Date;

import ua.at.tsvetkov.util.Log;
import android.app.Activity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import ua.at.tsvetkov.samizdat.Const;
import ua.at.tsvetkov.samizdat.R;
import ua.at.tsvetkov.samizdat.data.Data;
import ua.at.tsvetkov.samizdat.data.DataDownloader;
import ua.at.tsvetkov.samizdat.data.FavoriteAuthors;
import ua.at.tsvetkov.samizdat.data.FavoriteBooks;

/**
 * @author A.Tsvetkov 2011, http://tsvetkov.at.ua, al@ukr.net
 */
public class AdtBooks extends DataReaderAdapter {

	private FavoriteAuthors	favoriteAuthors;
	private FavoriteBooks	favoriteBooks;

	/**
	 * @param context
	 * @param dataReader
	 */
	public AdtBooks(Activity context, DataDownloader dataLoader, int partSize) {
		super(context, dataLoader, partSize);
		favoriteAuthors = new FavoriteAuthors(context);
		favoriteAuthors.load();
		favoriteBooks = new FavoriteBooks(context);
		favoriteBooks.load();
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View view = super.getView(position, convertView, parent);
		if (view != null)
			return view;

		BookViewHolder holder = new BookViewHolder();
		if (convertView == null || convertView.getTag() != holder) {
			convertView = mInflater.inflate(R.layout.adt_book, null);
			holder.author = (TextView) convertView.findViewById(R.id.AdtBookAuthor);
			holder.title = (TextView) convertView.findViewById(R.id.AdtBookTitle);
			holder.annotation = (TextView) convertView.findViewById(R.id.AdtBookAnnotation);
			holder.type = (TextView) convertView.findViewById(R.id.AdtBookType);
			holder.size = (TextView) convertView.findViewById(R.id.AdtBookSize);
			holder.raiting = (TextView) convertView.findViewById(R.id.AdtBookRating);
			holder.data = (TextView) convertView.findViewById(R.id.AdtBookData);
			holder.isNew = (TextView) convertView.findViewById(R.id.AdtBookIsNew);
			holder.favoriteBook = (ImageView) convertView.findViewById(R.id.AdtBookIsFavoriteComposition);
			holder.favoriteAuthor = (ImageView) convertView.findViewById(R.id.AdtBookIsFavoriteAuthor);
			convertView.setTag(holder);
		} else {
			holder = (BookViewHolder) convertView.getTag();
		}
		String[] fields = null;
		try {
			fields = dataLoader.getDataFields(position - 1, Data.BOOK_FIELDS_COUNT);
			holder.author.setText(fields[Data.BOOK_AUTHOR]);
			holder.title.setText(fields[Data.BOOK_TITLE].trim());
			String annotation = "";
//			if (fields.length == 8)
				annotation = fields[Data.BOOK_ANNOT];
			if (annotation != null && annotation.length() != 0) {
				holder.annotation.setText(annotation);
			}
			holder.type.setText(fields[Data.BOOK_TYPE]);
			String size = fields[Data.BOOK_SIZE];
			if (size != null && size.length() != 0) {
				holder.size.setText(size + " kb");
			}
			String ball = fields[Data.BOOK_BALL];
			if (ball != null && ball.length() != 0) {
				holder.raiting.setText(fields[Data.BOOK_COUNTER] + '*' + ball);
			}
			if (fields[Data.BOOK_DATE].length() > 0) {
				Date date = new Date(fields[Data.BOOK_DATE]);
				if (date.after(Const.CONTROL_DATE)) {
					holder.isNew.setText("New!");
				}
				holder.data.setText(fields[Data.BOOK_DATE]);
			} else {
				holder.isNew.setText("New!");
			}
			if (favoriteBooks.isFavorite(fields[Data.BOOK_OBJECT])) {
				holder.favoriteBook.setVisibility(View.VISIBLE);
			}
			if (favoriteAuthors.isFavorite(fields[Data.BOOK_AUTHOR])) {
				holder.favoriteAuthor.setVisibility(View.VISIBLE);
			}
		} catch (Exception e) {
			Log.d("Line " + position + ", полей-" + fields.length + " :" + dataLoader.getData(position - 1));
		}
		return convertView;
	}

	class BookViewHolder {
		TextView		author;
		TextView		title;
		TextView		annotation;
		TextView		type;
		TextView		size;
		TextView		raiting;
		TextView		data;
		TextView		isNew;
		ImageView	favoriteBook;
		ImageView	favoriteAuthor;
	}

	@Override
	public void refreshData() {
			favoriteAuthors.clear();
			favoriteBooks.clear();
			favoriteAuthors.load();
			favoriteBooks.load();
	}
}
